package com.Date1;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class Test5 {
    public static void main(String[] args) {
        //Calendar是一个抽象类，因此借助其子类创建对象
        Calendar cal = new GregorianCalendar();
        Calendar cal2 = Calendar.getInstance();
        SimpleDateFormat cal22 = new SimpleDateFormat("yyyy-MM-dd ");
        //要展示的东西太多了
        System.out.println(cal.toString());
        //得到相应的东西

        //get方法
        System.out.println(cal.get(Calendar.YEAR));
        System.out.println(cal.get(Calendar.MONTH));
        System.out.println(cal.get(Calendar.DATE));
        System.out.println(cal.get(Calendar.DAY_OF_MONTH));
        //获取当月日期的最大天数
        System.out.println(cal.getActualMaximum(Calendar.DATE));
        //获取当月日期的最小天数
        System.out.println(cal.getActualMinimum(Calendar.DATE));


        //Set方法
        cal2.set(Calendar.YEAR,1990);
        cal2.set(Calendar.MONTH,3);
        cal2.set(Calendar.DATE,16);
        System.out.println(cal2);
        //这里的cal222是字符串，而cal22是SimpleDateFormat类型，而cal2是Calendar类型的
        String cal222 = cal22.format(cal2.getTime());
        //将String类型转换为Calendar:可以先将String转化为（sql）Date;再将Date转化为Calendar
        System.out.println(cal222);
        java.sql.Date d1 = java.sql.Date.valueOf("2021-8-19");
        cal.setTime(d1);
        System.out.println(cal);
        //这里会显示的数字是7，因为这个格式会从0月开始
        System.out.println(cal.get(Calendar.MONTH));
        System.out.println(cal.get(Calendar.DATE));
    }
}
